const express = require('express');
const router = express.Router();
// 引入刚才定义的表
const Example_col = require('./../models/example');

/**
 * This function comment is parsed by doctrine
 * @route GET /example/get-mongo
 * @summary get示例 - MongoDB
 * @group example0 - 组0注释
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
router.get('/get-mongo', async (req, res, next) => {
  const examples = await Example_col.find({}, { _id: 0 });
  res.json({
    msg: 'get request!!',
    data: {
      // data: req,
      examples,
    }
  });
});

/**
 * This function comment is parsed by doctrine
 * @route POST /example/post-mongo
 * @summary post示例 - MongoDB
 * @group example0 - 组0注释
 * @param {String} msg.query - username
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
router.post('/post-mongo', async (req, res, next) => {
  const { msg } = req.query;

  res.status = 200;
  // if (!req.msg || typeof req.msg != 'string') {
  //   ctx.status = 401;
  //   ctx.body = {
  //     msg: 'post request!!',
  //     desc: `parameter error！！msg: ${req.msg}`,
  //     data: req
  //   }
  //   return;
  // }

  console.log(msg)

  // const result = await Example_col.create({msg});
  const result = await Example_col.find({ msg })
  // if (result[0])
  //   result2 = await Example_col.update({_id:result[0]._id})

  res.send({
    msg: 'post request!!',
    desc: 'insert success!',
    data: result
  })

  // res.send(result);
});

/**
 * This function comment is parsed by doctrine
 * @route POST /example/add-mongo
 * @summary 增加 - MongoDB
 * @group example - 组2注释
 * @param {String} msg.query - username
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
router.post('/add-mongo', async (req, res, next) => {
  const { msg } = req.query;
  console.log(msg)
  const result = await Example_col.create({ msg });

  res.send({
    msg: 'insert success!',
    data: result
  })
});

/**
 * This function comment is parsed by doctrine
 * @route POST /example/del-mongo
 * @summary 删除 - MongoDB
 * @group example - 组2注释
 * @param {String} _id.query - username
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
router.post('/del-mongo', async (req, res, next) => {
  const { _id } = req.query;

  console.log({ _id })

  const result = await Example_col.find({ _id });
  if (result[0])
    var result2 = await Example_col.deleteOne({ _id })

  res.send({
    msg: 'del success!',
    data: result,
    result2: result2
  })
});

/**
 * This function comment is parsed by doctrine
 * @route POST /example/update-mongo
 * @summary 改 - MongoDB
 * @group example - 组2注释
 * @param {String} _id.query - id
 * @param {String} msg.query - msg-
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
router.post('/update-mongo', async (req, res, next) => {
  const { _id, msg } = req.query;
  console.log({ _id, msg })

  const result = await Example_col.find({ _id });
  if (result[0])
    var result2 = await Example_col.update({ _id }, { msg })

  res.send({
    msg: 'del success!',
    data: result,
    data2: result2
  })
  // res.send({ data: result })
});

/**
 * This function comment is parsed by doctrine
 * @route GET /example/find-mongo
 * @summary 查 - MongoDB
 * @group example - 组2注释
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
router.get('/find-mongo', async (req, res, next) => {
  const result = await Example_col.find();
  res.send({
    msg: 'find success!',
    data: result
  })
});

/**
 * This function comment is parsed by doctrine
 * @route GET /example/findone-mongo
 * @summary 查一个 - MongoDB
 * @group example - 组2注释
 * @param {String} msg.query - username
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
router.get('/findone-mongo', async (req, res, next) => {
  const { msg } = req.query;
  console.log({ msg });

  const result = await Example_col.find({ msg });

  res.send({
    msg: 'find success!',
    data: result
  })
});

module.exports = router;
